<?php
class Turn extends AppModel {
	var $name = 'Turn';
	var $validate = array(
		'user_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);
	//The Associations below have been created with all possible keys, those that are not needed can be removed

	var $belongsTo = array(
		'Client' => array(
			'className' => 'Client',
			'foreignKey' => 'client_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Person' => array(
			'className' => 'Person',
			'foreignKey' => 'person_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Stateturn' => array(
			'className' => 'Stateturn',
			'foreignKey' => 'stateturn_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Staterecord' => array(
			'className' => 'Staterecord',
			'foreignKey' => 'staterecord_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'User' => array(
			'className' => 'User',
			'foreignKey' => 'user_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);
        
        
        /**
         * Obtiene los turnos dados
         * @param str $ver
         * @param int $client_id
         * @param int $profesional_id
         * @param int $agenda_seleccionada
         * @param datetime $consulta_desde
         * @param datetime $consulta_hasta
         * @return array
         */
        function getTurnos($ver='all', $client_id=0, $profesional_id=0, $agenda_seleccionada=0, $consulta_desde, $consulta_hasta){
            if ($ver == 'all') {
                $turnos = $this->find('all', array(
                    'conditions' => array(
                        'Turn.client_id' => $client_id,
                        'Turn.doctor_id' => $profesional_id,
                        'Turn.agenda_id' => $agenda_seleccionada,
                        'Turn.fecha BETWEEN ? AND ?' => array($consulta_desde, $consulta_hasta),
                    ),
                    'order' => array('Turn.fecha'),
                    'fields' => array('Person.id', 'Person.lastname', 'Person.name', 'Turn.fecha', 'Turn.id', 'Turn.nota', 'Turn.stateturn_id', 'Turn.confirm', 'Turn.sobreturno', 'Turn.producto_id'),
                        ));
            } else {
                $turnos = $this->find('all', array(
                    'conditions' => array(
                        'Turn.client_id' => $client_id,
                        'Turn.doctor_id' => $profesional_id,
                        'Turn.agenda_id' => $agenda_seleccionada,
                        'Turn.stateturn_id <>' => 3,
                        'Turn.fecha BETWEEN ? AND ?' => array($consulta_desde, $consulta_hasta),
                    ),
                    'order' => array('Turn.fecha'),
                    'fields' => array('Person.id', 'Person.lastname', 'Person.name', 'Turn.fecha', 'Turn.id', 'Turn.nota', 'Turn.stateturn_id', 'Turn.confirm', 'Turn.sobreturno', 'Turn.producto_id'),
                        ));
            }
            
            return $turnos;
        }
}
?>